package intermouse;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import javax.imageio.ImageIO;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

/**
 * @author Sőre András
 * @version 1.0
 * @since 2013-05-10
 *
 */
public class GraphicsManager extends javax.swing.JFrame{
	
		/**
		 * a singleton példány
		 */
		private static GraphicsManager instance=null;
		/**
		 * A GraphicsCanvasokat tartalmaző lista
		 */
		ArrayList<GraphicsCanvas> canvases;
		
		/**
		 * Megmondja hogy dezodort fújunk-e éppen
		 */
		private boolean deodorantOn;
		/**
		 * Megmondja hogy mérget fújunk-e éppen
		 */
		private boolean killerOn;
		/**
		 * számokat tartalmazó képeket tartalmazó lista
		 */
		private ArrayList<Image> imageNumbers = new ArrayList<Image>();
		
		/**
		 * @return the deodorantOn
		 */
		public boolean getDeodorantOn() {
			return deodorantOn;
		}

		/**
		 * @param deodorantOn the deodorantOn to set
		 */
		public void setDeodorantOn(boolean deodorantOn) {
			this.deodorantOn = deodorantOn;
		}

		/**
		 * @return the killerOn
		 */
		public boolean getKillerOn() {
			return killerOn;
		}

		/**
		 * @param killerOn the killerOn to set
		 */
		public void setKillerOn(boolean killerOn) {
			this.killerOn = killerOn;
		}

		
	
	/**
	 * Az osztály default konstruktora.
	 */
	protected GraphicsManager() {
		
		getContentPane().setFocusable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("INTERMOUSE_GAEM");
		getContentPane().setBackground(Color.WHITE);
		setBackground(Color.WHITE);
		setVisible(true);
		
		final JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		
		JButton btnUjJatek = new JButton("\u00DAj j\u00E1t\u00E9k");
		btnUjJatek.setFocusable(false);
		btnUjJatek.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				switch(JOptionPane.showInternalConfirmDialog(getContentPane(), "Új játékot indítasz?",
						"Új játék indítása", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)){
				case JOptionPane.NO_OPTION: break;
				case JOptionPane.YES_OPTION: Main.reset();	
				};
			}
		});
		btnUjJatek.setFont(new Font("Arial", Font.PLAIN, 22));
		btnUjJatek.setToolTipText("\u00DAj j\u00E1t\u00E9k ind\u00EDt\u00E1sa");
		btnUjJatek.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		btnUjJatek.setBackground(Color.WHITE);
		
		JButton btnKilepes = new JButton("Kil\u00E9p\u00E9s");
		
		btnKilepes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				switch(JOptionPane.showInternalConfirmDialog(getContentPane(), "Valóban ki akarsz lépni?",
						"Kilépés a játékból", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)){
				case JOptionPane.NO_OPTION: break;
				case JOptionPane.YES_OPTION: System.exit(0);	
				};
				
			}
		});
		btnKilepes.setFocusable(false);
		btnKilepes.setToolTipText("Kil\u00E9p\u00E9s a j\u00E1t\u00E9kb\u00F3l");
		btnKilepes.setFont(new Font("Arial", Font.PLAIN, 22));
		btnKilepes.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		btnKilepes.setBackground(Color.WHITE);
		
		lblPont = new JLabel("Pont: 9001");
		lblPont.setFocusable(false);
		lblPont.setFont(new Font("Arial", Font.PLAIN, 22));
		lblPont.setHorizontalAlignment(SwingConstants.CENTER);
		lblPont.setBorder(new LineBorder(new Color(0, 0, 0), 2));
		
		btnDeo = new JButton("66"){
			public void paintComponent(Graphics g){
				if(deodorantOn)
				setBackground(java.awt.Color.CYAN);
				else
				setBackground(java.awt.Color.WHITE);
				super.paintComponent(g);
			}
			
			private static final long serialVersionUID = 3518146801512754813L;
			
		};
		btnDeo.setHorizontalTextPosition(SwingConstants.CENTER);
		btnDeo.setVerticalTextPosition(SwingConstants.BOTTOM);
		btnDeo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(GameState.getInstance().getAntDeodorantSpraysLeft()>0)
				deodorantOn=!deodorantOn;		
			}
		});
		btnDeo.setFont(new Font("Arial", Font.PLAIN, 22));
		btnDeo.setFocusable(false);
		btnDeo.setIcon(new ImageIcon("pic/SprayDeodorant.png"));
		btnDeo.setBackground(Color.WHITE);
		btnDeo.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		
		btnKiller = new JButton("66"){
			public void paintComponent(Graphics g){
				if(killerOn)
				setBackground(java.awt.Color.GREEN);
				else
				setBackground(java.awt.Color.WHITE);
				super.paintComponent(g);
			}
			
			private static final long serialVersionUID = 3518146801512754813L;
			
		};
		btnKiller.setVerticalTextPosition(SwingConstants.BOTTOM);
		btnKiller.setHorizontalTextPosition(SwingConstants.CENTER);
		btnKiller.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
		
				if(GameState.getInstance().getAntKillerSpraysLeft()>0)
				killerOn=!killerOn;
				
			}
		});
		btnKiller.setFont(new Font("Arial", Font.PLAIN, 22));
		btnKiller.setFocusable(false);
		btnKiller.setIcon(new ImageIcon("pic/SprayKiller.png"));
		btnKiller.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		btnKiller.setBackground(Color.WHITE);
		
		JButton button = new JButton("?");
		button.setFocusable(false);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(getContentPane(), new JLabel("<html>A pálya görgetése a wasd gombokkal történik,"
							+" a sprék használatához a megfelelő gombra kell kattintani"
						 + ", majd a pálya egyik mezőjére.</html>"){
					
					private static final long serialVersionUID = -7421839344565599718L;

					{setPreferredSize(new Dimension(200,160));}
					}
				);
			}
		});
		button.setFont(new Font("Dialog", Font.BOLD, 19));
		button.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		button.setBackground(Color.WHITE);
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addComponent(lblPont, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addComponent(btnDeo, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnKiller, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(button, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnKilepes, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
						.addComponent(btnUjJatek, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE))
					.addGap(24))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnUjJatek, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(button, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
								.addComponent(btnKilepes, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
							.addComponent(lblPont, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(btnDeo, GroupLayout.PREFERRED_SIZE, 245, GroupLayout.PREFERRED_SIZE)
								.addComponent(btnKiller, GroupLayout.PREFERRED_SIZE, 245, GroupLayout.PREFERRED_SIZE)))
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE))
					.addContainerGap())
		);
		
		TreeMap<FieldTypes,Image> imageMap = new TreeMap<FieldTypes,Image>();
		try {
			imageMap.put(FieldTypes.Ant, ImageIO.read(new File("pic/40x46/Ant.png")));
			imageMap.put(FieldTypes.AntEater, ImageIO.read(new File("pic/40x46/AntEater.png")));
			imageMap.put(FieldTypes.AntHill, ImageIO.read(new File("pic/40x46/AntHill.png")));
			imageMap.put(FieldTypes.AntLion, ImageIO.read(new File("pic/40x46/AntLion.png")));
			imageMap.put(FieldTypes.AntSmell, ImageIO.read(new File("pic/40x46/AntSmell.png")));
			imageMap.put(FieldTypes.Field, ImageIO.read(new File("pic/40x46/Field.png")));
			imageMap.put(FieldTypes.FieldObject, ImageIO.read(new File("pic/40x46/FieldObject.png")));
			imageMap.put(FieldTypes.Food, ImageIO.read(new File("pic/40x46/Food.png")));
			imageMap.put(FieldTypes.FoodSmell, ImageIO.read(new File("pic/40x46/FoodSmell.png")));
			imageMap.put(FieldTypes.Poison, ImageIO.read(new File("pic/40x46/Poison.png")));
			imageMap.put(FieldTypes.Stone, ImageIO.read(new File("pic/40x46/Stone.png")));
			imageMap.put(FieldTypes.Water, ImageIO.read(new File("pic/40x46/Water.png")));
			for(int i=1; i<=9; i++){
				imageNumbers.add(ImageIO.read(new File("pic/40x46/"+i+".png")));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();	
		}
		
		
		final JPanel panel = new JPanel();
		panel.setLayout(null);
	
		canvases=new ArrayList<GraphicsCanvas>();
		
		for (int i = 0; i < 40; i++) {
			if(i%2==0){
				for (int j = 0; j < 60; j++) {
					GraphicsCanvas tmp = new GraphicsCanvas(imageMap,i,j);
					tmp.setBounds(20+40*j, i/2*68, 40, 46);
					panel.add(tmp);
					canvases.add(tmp);
					}
				}
			else{
				for (int j = 0; j < 60; j++) {
					GraphicsCanvas tmp = new GraphicsCanvas(imageMap,i,j);
					tmp.setBounds(40*j, 34+((i-1)/2)*68, 40, 46);
					panel.add(tmp);
					canvases.add(tmp);
				}
			}
		}
	
	
		panel.setBackground(Color.WHITE);
		panel.setPreferredSize(new Dimension(2420, 1372));
		scrollPane.setViewportView(panel);
		panel.setLayout(null);
		
		deodorantOn=false;
		killerOn=false;
		
		getContentPane().setLayout(groupLayout);
		
		addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent arg0) {
				switch(arg0.getKeyChar()){
				case 's':
					scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getValue()+60); break;
				case 'w':
					scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getValue()-60); break;
				case 'a':
					scrollPane.getHorizontalScrollBar().setValue(scrollPane.getHorizontalScrollBar().getValue()-100); break;
				case 'd':
					scrollPane.getHorizontalScrollBar().setValue(scrollPane.getHorizontalScrollBar().getValue()+100); break;
				}
			}
		});
	}
	
	/**
	 * @param x
	 * @param y
	 * @param list
	 * GraphicsCanvas FieldTypes mezőjét az aktuálisra frissítő függvény
	 */
	public void drawField(int x, int y, List<FieldTypes> list){
		canvases.get(x*60+y).setList(list);
		canvases.get(x*60+y).repaint();
	}
	
	/**
	 * @return
	 * Singleton példányt visszaadó függvény
	 */
	public static GraphicsManager getInstance(){
		if(instance==null)
			instance = new GraphicsManager();
		return instance;
	}
	
	/**
	 * Pontszámokat és sprék számát frissítő függvény
	 */
	public void updateWidget(){
		lblPont.setText("Pont:" + GameState.getInstance().getPlayerScore());
		btnDeo.setText(Integer.toString(GameState.getInstance().getAntDeodorantSpraysLeft()));
		btnKiller.setText(Integer.toString(GameState.getInstance().getAntKillerSpraysLeft()));
	}
	
	/**
	 * @param i
	 * @return
	 * számhoz számot tartalmaző képet visszaadó függvény
	 */
	public Image getNumberImage(int i){
		return imageNumbers.get(i-1);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = -4696772493665004292L;
	private JLabel lblPont;
	private JButton btnKiller;
	private JButton btnDeo;
}
